package com.shop.cloud.controller.hongfu;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Activity;
import com.shop.cloud.domain.hongfu.ActivityCategory;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.filter.IgnoreAuth;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.ActivityCategoryService;
import com.shop.cloud.service.hongfu.IActivityService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 活动管理
 * @author Yan Weiwei
 *
 */
@RestController	
@RequestMapping(value = "api")
public class ActivityController extends BaseController {

	@Autowired
	private IActivityService activityService;
	

	@Autowired
	private ActivityCategoryService activityCategoryService;
	
	@IgnoreAuth
	@ApiOperation(value = "获取活动列表", notes = "获取活动列表")
	@RequestMapping(value = "/activityList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Activity>> activityList(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		activityService.handleActivityStart();
		activityService.handleActivityEnd();
		//app价格price字段筛选条件 null：全部 0：免费 1：付费
		
		activity.setExistFlag(0);
		activity.setOrderBy("ACTIVITY.status");
		Page<Activity> data = activityService.page(activity);
		
		return new ObjectDataResponse<Page<Activity>>(data);
	}
	
	@ApiOperation(value = "获取我的活动列表", notes = "获取我的活动列表")
	@RequestMapping(value = "/myActivityList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Activity>> myActivityList(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		activity.setOrderBy("ACTIVITY_ENROLL.enroll_time DESC");
		Page<Activity> data = activityService.getPage(userId, activity);
		
		return new ObjectDataResponse<Page<Activity>>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "获取某个活动", notes = "获取某个活动")
	@RequestMapping(value = "/getOneActivity", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Activity> getActivity(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		Activity insActivity = activityService.handleActivity(userId, activity.getId().toString());
		
		return new ObjectDataResponse<Activity>(insActivity);
	}
	
	@ApiOperation(value = "报名某个活动", notes = "报名某个活动")
	@RequestMapping(value = "/handleActivityEnroll", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handleActivityEnroll(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		int data = activityService.handleActivityEnroll(userId, activity.getId().toString());
		
		return new ObjectDataResponse<Integer>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "活动分类列表", notes = "活动分类列表")
	@RequestMapping(value = "/activityCategory", method = RequestMethod.POST)
	@ResponseBody
	public Response activityCategory(@RequestBody ActivityCategory activityCategory){
		

		activityCategory.setOrderBy("ACTIVITY_CATEGORY.weigh_num");
		
		Page<ActivityCategory> data = activityCategoryService.page(activityCategory);
		
		if (data.getData().size()>0) {
			return ResponseUtil.ok(data.getData());
		}
		
		return ResponseUtil.ok(data);
	}
	
	
}
